<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            font-size: 20px;
        }

        .box {
            width: 800px;
            height: 60px;
            margin: 220px auto;
            position: relative;
        }

        #txt {
            width: 798px;
            height: 58px;
            border: 1px solid #ccc;
            font-size: 20px;
        }


        #keywords {
            position: absolute;
            top: 60px;
            left: 0;
            background-color: rgb(12, 255, 24);
            list-style: none;
            width: 798px;
            ;
        }

        li {
            line-height: 24px;
            cursor: pointer;
        }

        #description {
            /* display: none; */
            width: 798px;
            height: 200px;
            position: absolute;
            top: -210px;
            border: 1px dashed green;
        }

        .go {
            width: 100px;
            height: 40px;
            line-height: 40px;
            background-color: burlywood;
            box-shadow: cadetblue;
            position: absolute;
            text-align: center;
            left: 50px;
            top: 100px;
        }

        .go:hover {
            cursor: pointer;
            color: steelblue;
        }
    </style>
</head>

<body>
    <div class="go">
        单词存储
    </div>
    <div class="box">
        <div id="description"></div>
        <div class="top">
            <input type="text" id="txt" placeholder="请输入要检索的内容，可以是单词也可以是解释">
        </div>
        <ul id="keywords"></ul>
    </div>
    <script>
        let txt = document.getElementById('txt');
        let des = document.getElementById('description');
        let ul = document.getElementById('keywords');
        txt.oninput = function () {
            let value = txt.value.trim();
            if (value == '') return;
            ul.innerHTML = '';
            let words = JSON.parse(localStorage.getItem('words'));
            // console.log(words);
            for (let key in words) {
                // console.log(key, words[key]);
                if (key.indexOf(value) != -1 || words[key].indexOf(value) != -1) {
                    let li = document.createElement('li');
                    li.innerHTML = `${key}: ${words[key]}`;
                    ul.append(li);
                    li.onmouseover = function () {
                        li.style.backgroundColor = 'yellow';
                    };
                    li.onmouseout = function () {
                        li.style.backgroundColor = '';
                    };
                    li.onclick = function () {
                        des.innerText = li.innerText;
                        txt.value = '';
                        ul.innerHTML = '';
                    };
                };
            };
        };
    </script>
</body>

</html>